package com.gec.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gec.system.mapper.SysRoleMapper;
import com.gec.system.mapper.SysUserRoleMapper;
import com.gec.system.service.SysMenuService;
import com.gec.system.system.SysRole;
import com.gec.system.system.SysUser;
import com.gec.system.system.SysUserRole;
import com.gec.system.vo.AssignRoleVo;
import com.gec.system.vo.RouterVo;
import com.gec.system.vo.SysUserQueryVo;
import com.gec.system.mapper.SysUserMapper;
import com.gec.system.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author dafei
 * @since 2024-09-21
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysMenuService menuService;

    @Override
    public IPage<SysUser> selectPage(IPage<SysUser> page, SysUserQueryVo vo) {
        return this.baseMapper.selectPage(page,vo);
    }

    @Override
    public boolean updateStauts(SysUser user) {
        return this.baseMapper.updateStatusInteger(user);
    }




    @Override
    public Map<String, Object> getRolesByUserId(Long userId) {
        //获取所有角色
        List<SysRole> sysRoles = roleMapper.selectList(null);
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        /**
         *         user_id wrapper 相当于过滤
         *         寻早user_id为userId的数据
         */
        wrapper.eq("user_id",userId);
        // 获取sys_user_role的为userId的数据并存放在list
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(wrapper);
        ArrayList<Long> userRoleIds = new ArrayList<>();
        for (SysUserRole userRole : sysUserRoles) {
            // 获取user_id的不同role_id
            userRoleIds.add(userRole.getRoleId());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("allRoles",sysRoles);
        map.put("userRoleIds",userRoleIds);
        return map;
    }

    @Override
    public void doAssign(AssignRoleVo assignRoleVo) {
//        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
//        // 前端值映射
//        wrapper.eq("user_id",assignRoleVo.getUserId());
//        //删除前端映射的user_id
//        sysUserRoleMapper.delete(wrapper);
        sysUserRoleMapper.deleteById(assignRoleVo.getUserId());
        List<Long> userRoleIds = assignRoleVo.getUserRoleIds();
        for (Long roleId : userRoleIds) {
            if(roleId!=null){
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(assignRoleVo.getUserId());
                sysUserRole.setRoleId(roleId);
                sysUserRoleMapper.insert(sysUserRole);
            }
        }


    }

    @Override
    public SysUser getUserInfoUserName(String username) {
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.eq("username",username);
        return baseMapper.selectOne(sysUserQueryWrapper);
    }

    @Override
    public Map<String, Object> getUserInfo(String username) {
        HashMap<String, Object> map = new HashMap<>();
        SysUser sysUser = this.getUserInfoUserName(username);
        log.warn(String.valueOf(sysUser.getId()));
        List<RouterVo> routerVoList = menuService.findUserMenuList(sysUser.getId());
        System.out.println(routerVoList);
        //根据用户id获取用户按钮权限
        List<String> permsList = menuService.findUserPermsList(sysUser.getId());
        System.out.println(permsList);
        map.put("name", sysUser.getName());
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("roles",  "[admin]");


        map.put("buttons", permsList);
        map.put("routers", routerVoList);
        return map;
    }




}
